﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace SHWARSICO.Gaming.XNASter._2D
{
    /// <summary>
    /// Represents 2D Gaming Object Class
    /// </summary>
    public class GObject
    {
        protected string res_name;
        protected Game game;

        Texture2D texture;

        /// <summary>
        /// Position of GObject
        /// </summary>
        public Vector2 Pos;

        /// <summary>
        /// Velocity of GObject
        /// </summary>
        public Vector2 Velocity;

        /// <summary>
        /// Acceleration of GObject
        /// </summary>
        public Vector2 Accel = Vector2.Zero;

        /// <summary>
        /// Friction coefficient - on each cycle velocity if multiplied by Friction
        /// </summary>
        public float Friction = 1.0f;

        /// <summary>
        /// All GObject present in the game
        /// </summary>
        public static List<GObject> AllObjects = new List<GObject>();

        public GObject(Game G, string res_name)
        {
            this.res_name = res_name;
            game = G;
            Pos = Velocity = Vector2.Zero;
            AllObjects.Add(this);
        }

        public void LoadContent()
        {
            texture = game.Content.Load<Texture2D>(res_name);
        }

        public static void LoadContentAll()
        {
            foreach (var o in AllObjects) o.LoadContent();
        }

        public static void UpdateAll()
        {
            foreach (var o in AllObjects) o.Update();
        }

        public static void DrawAll(SpriteBatch B)
        {
            foreach (var o in AllObjects) o.Draw(B);
        }

        public void Update()
        {
            Pos += Velocity;
            Velocity = Friction * Velocity + Accel;
            if (Pos.X < texture.Width / 2) { Pos.X = texture.Width / 2; Velocity.X = -Velocity.X; }
            if (Pos.Y < texture.Height / 2) { Pos.Y = texture.Height / 2; Velocity.Y = -Velocity.Y; }
            if (Pos.X > game.GraphicsDevice.Viewport.Width - texture.Width / 2)
            { Pos.X = game.GraphicsDevice.Viewport.Width - texture.Width / 2; Velocity.X = -Velocity.X; }
            if (Pos.Y > game.GraphicsDevice.Viewport.Height - texture.Height / 2)
            { Pos.Y = game.GraphicsDevice.Viewport.Height - texture.Height / 2; Velocity.Y = -Velocity.Y; }
        }

        public void Draw(SpriteBatch B)
        {
            var v = new Vector2(Pos.X - texture.Width / 2, Pos.Y - texture.Height / 2);
            B.Draw(texture, v, Color.White);
        }

        public static bool Close(GObject G1, GObject G2, float dist)
        {
            return Vector2.Distance(G1.Pos, G2.Pos) < dist;
        }

        public void Randomize(Random R, int margin)
        {
            Pos.X = R.Next(margin + texture.Width / 2, game.GraphicsDevice.Viewport.Width - margin - texture.Width / 2);
            Pos.Y = R.Next(margin + texture.Height / 2, game.GraphicsDevice.Viewport.Height - margin - texture.Height / 2);
        }

    }

}
